
var numFixed = function(val, decimal){
	// 转换数字为指定小数位的字符串
	if (val == '--')
		return '--';
    if(val == null)
        return '';
    if(val == 0)
        return val;
	  val = parseFloat(val);
    val = val.toFixed(decimal);
    // while(val.endsWith('0'))
    //     val = val.slice(0, -1);
    return val;
};


var chainCalculate = function(val, val1){
	// 计算环比，同比 .....
	return (val1-val)/val;
	// type[DOD,WOW,MOM,QOQ,YOY]
}

var pingAnChain = function(data){
	// data 包含value, comparevalue, comparetype 三个属性
	// 返回data， 增加了 chaindata 属性，是为计算好的 同比/环比值
	// chaindata = [{"label": "两融成交量", "value": 0.1, "vdate": 20150901},
    //  {"label": "传统成交量", "value": 0.1, "vdate": 20150901 },
 	//  {"label": "两融成交量", "value": 0.3, "vdate": 20150902 },
	//  {"label": "传统成交量", "value": 0.2, "vdate": 20150902 }, ]
	if(data.hasOwnProperty('comparetype') == false){
		return data;
	}
	var chaindata = [];

	// 获得value中全部时间
	var times = [];
	var timeValues = {};
	_(data.value).forEach(function(item){
		if(item.hasOwnProperty('vdate')){
			if(times.indexOf(item.vdate)<0){
				times.push(item.vdate);
				timeValues[item.vdate] = item;
			}
			// timeValues[item.vdate].push(item);
		}
		else{
			times = [null];
			timeValues[null] = item;
		}
	}).value()
	// 获得 comparevalue 中的全部时间
	var comparetimes = [];
	var comparetimeValues = {};
	_(data.comparevalue).forEach(function(item){
		if(item.hasOwnProperty('vdate')){
			if(comparetimes.indexOf(item.vdate)<0){
				comparetimes.push(item.vdate);
				comparetimeValues[item.vdate] = item;
			}
		}
		else{
			comparetimes = [null];
			comparetimeValues[null] = item;
		}
	}).value()
	comparetimes.sort();
	comparetimes.reverse();

	// 准备同比计算的日期组
	var timeCal = {};
	_(times).forEach(function(time){
		if(comparetimes.indexOf(time)>=0){
			timeCal[time] = time;
		}
		else{
			for(var i=0; i<comparetimes.length; i++){
				var ctime = comparetimes[i];
				if(ctime < time){
					timeCal[time] = ctime;
					break;
				}
			}
		}
	}).value()

	// 计算同比
	_(times).forEach(function(time){
		if(timeCal.hasOwnProperty(time) == false){
			return
		}
		var ctime = timeCal[time];
		var cvalue = comparetimeValues[ctime];
		var value = timeValues[time];
		var chainV = {};
		if (value['portion_volume']) {
			value['portion_volume'] = value['portion_total'] ? value['portion_volume'] / value['portion_total'] : 0;
			cvalue['portion_volume'] = cvalue['portion_total'] ? cvalue['portion_volume'] / cvalue['portion_total'] : 0;
		}
		if (value['commission_income']) {
			value['commission_rate'] = value['trade_volume'] ? value['commission_income'] / value['trade_volume'] : 0;
			cvalue['commission_rate'] = cvalue['trade_volume'] ? cvalue['commission_income'] / cvalue['trade_volume'] : 0;
		}
		_.forOwn(value, function(v, k){
			if(k == 'vdate' || k == 'yyyymmdd'){
				chaindata[k] = v;
			}
			if(cvalue.hasOwnProperty(k) == false){
				return
			}
			var result = (v-cvalue[k])/cvalue[k];
			if(result == Infinity || isNaN(result)){
				result = null;
			}
			chainV[k] = result
			// console.log(k, data.comparetype, chainV[k]);
		})
		chaindata.push(chainV);
	}).value()	

	data['chaindata'] = chaindata;
	return data
}


var unitFormat = function(val, unit){
	// 如果传入 unit, 使用 unit 单位处理数据
	if(val == null || val == '' || val == '--'){
		return [val, ''];
	}

	if(unit=='亿'){
		return [val / 100000000.0, '亿'];
	}
	else if(unit=='千万'){
		return [val / 10000000.0, '千万'];
	}
	else if(unit=='百万'){
		// 百万
		return [val / 1000000.0, '百万'];
	}
	else if(unit == '万'){
		return [val / 10000.0, '万']
	}
	else if(unit == ''){
		return [val, ''];
	}
	

	if(val / 100000000 >= 1){
		return [val / 100000000.0, '亿'];
	}
	else if(val / 10000000 >= 1){
		return [val / 10000000.0, '千万'];
	}
	else if(val / 1000000 >= 1){
		// 百万
		return [val / 1000000.0, '百万'];
	}
	else if(val / 10000 >= 1){
		return [val / 10000.0, '万']
	}
	else{
		return [val, ''];
	}
};


export { numFixed, pingAnChain, unitFormat };